package com.copera.cn.kaka;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.copera.cn.kaka.util.ActionUrls;
import com.copera.cn.kaka.util.ActivityManager;
import com.copera.cn.kaka.util.HttpUtils;
import com.copera.cn.kaka.view.ReportView;

public class MyShopExchangeActivity extends BaseActivity{

	private Button goback;
	List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>();
	private TextView tv;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ActivityManager.getInstance().push(this);
		setContentView(R.layout.activity_myshop_exchange);
		goback = (Button)findViewById(R.id.msex_bt_img);
        tv = (TextView)findViewById(R.id.msextv1);
		ReportView reportView = (ReportView) this.findViewById(R.id.myView);
        String[] arrX = new String[] { "0","2","4","6","8","10","12","14","16","18","20","22","24"};
        //String[] arrY = new String[] { "", "1", "2", "3", "4", "5","6" };
        int[] arrV = findTrackToday();
        try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} 
        reportView.SetInfo(arrX, arrV, "时");
        
        initEvent();
        SimpleAdapter simpleAdapter = new SimpleAdapter(this
                , listItems
                , R.layout.exchange_list
                , new String[]{"worker_name","name","date","content"}
                , new int[]{R.id.worker_name,R.id.name,R.id.date,R.id.tv});
        ListView listView = (ListView)findViewById(R.id.exchange_list);
		listView.setAdapter(simpleAdapter);
		setListViewHeightBasedOnChildren(listView);
		//强制为竖屏 
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}
	
	private void initEvent(){
		
		goback.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				finish();
			}
		});
	}
	//查询今天兑换的礼品
    private int[] findTrackToday(){
    	final int[] arrV = new int[]{0,0,0,0,0,0,0,0,0,0,0,0,0};
    	new Thread() {
			@SuppressLint("SimpleDateFormat")
			public void run() {
				try {
					String json = HttpUtils.get(ActionUrls.URL_EXCHANGERECORD_FINDEXCHANGETODAY+"?shopId="+getAppCustomer().getShop().getShopId(), getAppCustomer().getHttpClient());
					String dateStr = "";
					Integer dateNum = 0;
					Integer num = 0;
					JSONObject jo = new JSONObject(json);
					JSONArray jsonArr = jo.getJSONArray("exchangeList");
					tv.setText("今天兑换的礼品（"+getAppCustomer().getShop().getExchangeToday()+"）");
					if (jsonArr != null && jsonArr.length() > 0) {
						Map<String,Object> listItem ;
						for (int i = 0; i < jsonArr.length(); i++) {
							listItem = new HashMap<String,Object>();
							JSONObject object = jsonArr.getJSONObject(i);
							dateStr = object.getString("exchangeDateStr").substring(11, 13);
							dateNum = Integer.parseInt(dateStr);
							if (0 <= dateNum && dateNum < 2) {
								arrV[0] = arrV[0] + 1;
							}else if (2 <= dateNum && dateNum < 4){
								arrV[1] = arrV[1] + 1;
							}else if (4 <= dateNum && dateNum < 6){
								arrV[2] = arrV[2] + 1;
							}else if (6 <= dateNum && dateNum < 8){
								arrV[3] = arrV[3] + 1;
							}else if (8 <= dateNum && dateNum < 10){
								arrV[4] = arrV[4] + 1;
							}else if (10 <= dateNum && dateNum < 12){
								arrV[5] = arrV[5] + 1;
							}else if (12 <= dateNum && dateNum < 14){
								arrV[6] = arrV[6] + 1;
							}else if (14 <= dateNum && dateNum < 16){
								arrV[7] = arrV[7] + 1;
							}else if (16 <= dateNum && dateNum < 18){
								arrV[8] = arrV[8] + 1;
							}else if (18 <= dateNum && dateNum < 20){
								arrV[9] = arrV[9] + 1;
							}else if (20 <= dateNum && dateNum < 22){
								arrV[10] = arrV[10] + 1;
							}else{
								arrV[11] = arrV[11] + 1;
							}
							listItem.put("worker_name", object.getString("exchangeUser"));
							listItem.put("name", "");
							listItem.put("date", object.getString("exchangeDateStr").substring(11,16));
							listItem.put("content", object.getString("exchangeContent"));
							listItems.add(listItem);
						}
					}
				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (JSONException e) {
					e.printStackTrace();
				} 
			};
		}.start();
		return arrV;
    }
public void setListViewHeightBasedOnChildren(ListView listView) {   
    	
    	// 获取ListView对应的Adapter   
        ListAdapter listAdapter = listView.getAdapter();   
        if (listAdapter == null) {   
            return;   
        }   
    	   
    	int totalHeight = 0;   
    	for (int i = 0, len = listAdapter.getCount(); i < len; i++) {   
    		// listAdapter.getCount()返回数据项的数目   
	        View listItem = listAdapter.getView(i, null, listView);   
	        // 计算子项View 的宽高   
	        listItem.measure(0, 0);    
	        // 统计所有子项的总高度   
	        totalHeight += listItem.getMeasuredHeight();    
	    }          
       
        ViewGroup.LayoutParams params = listView.getLayoutParams();   
    	    params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));   
    	    // listView.getDividerHeight()获取子项间分隔符占用的高度   
    	    // params.height最后得到整个ListView完整显示需要的高度   
    	    listView.setLayoutParams(params);   
    }    
}
